import pygame
import math

# 初始化pygame
pygame.init()

# 定义常量
WIDTH, HEIGHT = 800, 600
BALL_RADIUS = 10
GOAL_WIDTH = 200
GOAL_HEIGHT = 100
GOAL_POSITION = (WIDTH // 2 - GOAL_WIDTH // 2, 20)
BALL_INIT_POSITION = (WIDTH // 2, HEIGHT - 50)
GOALKEEPER_WIDTH = 50
GOALKEEPER_HEIGHT = 80
GOALKEEPER_INIT_POSITION = (WIDTH // 2 - GOALKEEPER_WIDTH // 2, GOAL_POSITION[1] + GOAL_HEIGHT - GOALKEEPER_HEIGHT)
MAX_POWER = 20
SHOOTING_ATTEMPTS = 5

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("足球射门游戏")

# 颜色定义
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

# 初始化变量
ball_position = list(BALL_INIT_POSITION)
ball_velocity = [0, 0]
is_shooting = False
start_time = 0
score = 0
attempts_left = SHOOTING_ATTEMPTS

# 绘制足球场
def draw_field():
    screen.fill(GREEN)
    pygame.draw.rect(screen, WHITE, (GOAL_POSITION[0], GOAL_POSITION[1], GOAL_WIDTH, GOAL_HEIGHT), 5)
    pygame.draw.circle(screen, WHITE, BALL_INIT_POSITION, BALL_RADIUS)

# 绘制足球
def draw_ball():
    pygame.draw.circle(screen, RED, (int(ball_position[0]), int(ball_position[1])), BALL_RADIUS)

# 绘制守门员
def draw_goalkeeper(position):
    pygame.draw.rect(screen, BLUE, (position[0], position[1], GOALKEEPER_WIDTH, GOALKEEPER_HEIGHT))

# 检查球是否进入球门
def is_goal():
    return (
        GOAL_POSITION[0] < ball_position[0] < GOAL_POSITION[0] + GOAL_WIDTH and
        GOAL_POSITION[1] < ball_position[1] < GOAL_POSITION[1] + GOAL_HEIGHT
    )

# 检查球是否被守门员拦截
def is_blocked(goalkeeper_position):
    return (
        goalkeeper_position[0] < ball_position[0] < goalkeeper_position[0] + GOALKEEPER_WIDTH and
        goalkeeper_position[1] < ball_position[1] < goalkeeper_position[1] + GOALKEEPER_HEIGHT
    )

# 主游戏循环
clock = pygame.time.Clock()
running = True
while running:
    screen.fill(GREEN)
    draw_field()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN and attempts_left > 0:
            is_shooting = True
            start_time = pygame.time.get_ticks()
        elif event.type == pygame.MOUSEBUTTONUP and is_shooting:
            end_time = pygame.time.get_ticks()
            click_duration = (end_time - start_time) / 1000
            power = min(click_duration * 10, MAX_POWER)

            mouse_x, mouse_y = pygame.mouse.get_pos()
            dx = mouse_x - ball_position[0]
            dy = mouse_y - ball_position[1]
            distance = math.sqrt(dx ** 2 + dy ** 2)
            if distance > 0:
                ball_velocity[0] = dx / distance * power
                ball_velocity[1] = dy / distance * power

            is_shooting = False
            attempts_left -= 1

    if ball_velocity[0] != 0 or ball_velocity[1] != 0:
        ball_position[0] += ball_velocity[0]
        ball_position[1] += ball_velocity[1]

        # 守门员移动
        goalkeeper_position = list(GOALKEEPER_INIT_POSITION)
        if ball_position[0] < goalkeeper_position[0] + GOALKEEPER_WIDTH // 2:
            goalkeeper_position[0] -= 5
        elif ball_position[0] > goalkeeper_position[0] + GOALKEEPER_WIDTH // 2:
            goalkeeper_position[0] += 5
        draw_goalkeeper(goalkeeper_position)

        # 检查球是否出界
        if (
            ball_position[0] < 0 or ball_position[0] > WIDTH or
            ball_position[1] < 0 or ball_position[1] > HEIGHT
        ):
            ball_position = list(BALL_INIT_POSITION)
            ball_velocity = [0, 0]

        # 检查是否进球或被拦截
        if is_goal() and not is_blocked(goalkeeper_position):
            score += 1
            ball_position = list(BALL_INIT_POSITION)
            ball_velocity = [0, 0]
        elif is_blocked(goalkeeper_position):
            ball_position = list(BALL_INIT_POSITION)
            ball_velocity = [0, 0]
    else:
        draw_goalkeeper(GOALKEEPER_INIT_POSITION)

    draw_ball()

    # 显示得分和剩余尝试次数
    font = pygame.font.Font(None, 36)
    score_text = font.render(f"得分: {score}", True, WHITE)
    attempts_text = font.render(f"剩余尝试次数: {attempts_left}", True, WHITE)
    screen.blit(score_text, (10, 10))
    screen.blit(attempts_text, (10, 50))

    pygame.display.flip()
    clock.tick(60)

# 退出游戏
pygame.quit()